/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: HP
 * Date: 2023 -03 -06
 * Time: 17:37
 */
import java.io.*;

public class FileParse {
    public static void fileParsing(String fileDIR) {
        int cntLines=0,cntWords=0,cntSentences=0;
        try {
            InputStream is = new FileInputStream(fileDIR);
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String str_line;
            while ((str_line = br.readLine()) != null) {
                cntLines++;
                String[] temp=str_line.split(" ");
                cntWords+=temp.length;
                for (String s:temp) {
                    if(s.charAt(s.length()-1)=='.'||s.charAt(s.length()-1)=='?'||s.charAt(s.length()-1)=='!'){
                        cntSentences++;
                    }else if(s.charAt(s.length()-1)=='"'&&(s.charAt(s.length()-2)=='.'||s.charAt(s.length()-2)=='?'||s.charAt(s.length()-2)=='!')){
                        cntSentences++;
                    }
                }
            }
            br.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("读取文件行存储操作失败！");
            e.printStackTrace();
        }
        System.out.println("该文本单词数为:"+cntWords);
        System.out.println("该文本句子数为:"+cntSentences);
        System.out.println("该文本段落数为:"+cntLines);
    }
    public static void main(String[] args) {
        fileParsing("C:\\Users\\HP\\Desktop\\mo.txt");
    }
}

